package com.gg.redisbootservice.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author gg
 * @version SecondRedisConfig.java, v 0.1 2019-04-04 15:32 gg
 */
@Configuration
public class SecondRedisConfig extends RedisConfig {

    @Value("${spring.redis-two.database}")
    private int dbIndex;

    @Value("${spring.redis-two.host}")
    private String host;

    @Value("${spring.redis-two.port}")
    private int port;

    @Value("${spring.redis-two.password}")
    private String password;

    @Value("${spring.redis-two.timeout}")
    private int timeout;

    /**
     * 配置redis连接工厂
     *
     * @return RedisConnectionFactory
     */
    private RedisConnectionFactory secondRedisConnectionFactory() {
        return createJedisConnectionFactory(dbIndex, host, port, password, timeout);
    }

    /**
     * 配置redisTemplate 注入方式使用@Resource(name="") 方式注入
     *
     * @return RedisTemplate
     */
    @Bean(name = "secondRedisTemplate")
    public RedisTemplate<String,Object> secondRedisTemplate() {
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(secondRedisConnectionFactory());
        setSerializer(template);
        template.afterPropertiesSet();
        return template;
    }
}